package com.fingard.dsp.bank.directbank.hrbb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.hrbb01.util.Base64Util;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpReceiver;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

public class HRBB01Base extends DirectBase implements ITcpClient {
//    protected String[] retSign = new String[]{"", ""};
    protected String signedData = "";
    protected String[] tmpRets = new String[]{"", "", ""};
    protected String reqTime = "";

    protected void writeCommonHeader(XmlTextWriter xmlWriter, HRBB01Header header) throws Exception {
        if (StringHelper.isNullOrEmpty(header.bsnCode)) {
            throw new Exception("未赋值bsnCode");
        }
        if (StringHelper.isNullOrEmpty(header.serialNo)) {
            throw new Exception("未赋值serialNo");
        }

        xmlWriter.writeStartDocByDeclaration(header.bsnCode + "|" + bankFront.org + "|" + getBankConfig().getValueAsString("channel") + "|#<?xml version=\"1.0\" encoding = \"GBK\"?>");
        xmlWriter.writeStartElement("BankData");
        xmlWriter.writeStartElement("opReq");
        xmlWriter.writeElementString("bsnCode", header.bsnCode);
        if (addCstNo(header.bsnCode)) {
            xmlWriter.writeElementString("cstNo", bankFront.org);
        }
        xmlWriter.writeElementString("serialNo", header.serialNo);
        reqTime = Format.DateTime14Format.format(new Date());
        xmlWriter.writeElementString("reqTime", reqTime);
    }

    private boolean addCstNo(String bsnCode) {
        return "CBE004".equals(bsnCode) || "CBE005".equals(bsnCode) || "CBE006".equals(bsnCode) ||"CBE007".equals(bsnCode) || "CBE008".equals(bsnCode)
                || "CBE009".equals(bsnCode) || "CBE011".equals(bsnCode);
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {
        String[] retStr = new String[]{"", ""};

        WriteBankLogLn("1.提交银行的地址：" + bankFront.serverURL);
        WriteBankLogStep2(p_reqStr);
        WebRequest tmpWebRequest = new WebRequest(bankFront.serverURL);
        tmpWebRequest.setConnectTimeout(60000);
        tmpWebRequest.setRequestProperty("Content-Type", "text/xml;charset=GBK");
        tmpWebRequest.setRequestProperty("connection", "Close");
        String charsetName = getCharset();
        //请求报文需要进行Base64加密
        String reqStr = Base64Util.compressData(p_reqStr);
        retStr = tmpWebRequest.upload(reqStr.getBytes(charsetName), charsetName);
        WriteBankLogStep3(retStr[1]);

        return retStr;
    }

    //进行签名
    protected boolean signature(String data) throws Exception {
        String[] retSign = new String[]{"", ""};
        String signURL = bankFront.signURL;
        WriteBankLogLn("1.银行签名地址：" + signURL);
        WriteBankLogStep2(data);
        WebRequest tmpWebRequest = new WebRequest(signURL);
        tmpWebRequest.setConnectTimeout(60000);
        tmpWebRequest.setRequestProperty("Content-Length", String.valueOf(data.length()));
        tmpWebRequest.setRequestProperty("Content-Type", "INFOSEC_SIGN/1.0");
        //String charsetName = getCharset();
        byte[] tmpToWriteBytes = data.getBytes("utf-8");
        retSign = tmpWebRequest.upload(tmpToWriteBytes, "utf-8");

        if (retSign[0].length() == 0) {
            try {
                WriteBankLogLn("3.签名返回的结果：" + retSign[1]);
                XmlTextReader tmpXmlReader = new XmlTextReader(retSign[1]);
                String tmpRest = tmpXmlReader.getSimpleText("result");
                if ("0".equals(tmpRest)) {
                    WriteBankLogLn("签名成功");
                    signedData = tmpXmlReader.getSimpleText("sign");
                    return true;
                } else {
                    WriteBankLogLn("签名失败");
                    return false;
                }
            } catch (Exception e) {
                e.printStackTrace();
                WriteBankLogLn(e.getMessage());
            }

        } else {
            WriteBankLogLn("与银行签名端口通讯失败；详细信息：" + retSign[1]);
        }
        return false;
    }

//    protected String getDataToSign(XmlTextWriter xmlWriter) throws Exception {
//        String data = bsnCode + "|" + bankFront.org + "|" + channel + "|#" + xmlWriter.toXmlString();
//        //请求报文需要进行Base64加密
//        return Base64Util.stringToBase64(data);
//    }

    protected boolean getRet(String p_ret) {
        tmpRets = p_ret.split("\\|");
        if (tmpRets.length >= 3) {
            if ("0000000".equals(tmpRets[0])) {
                tmpRets[2] = tmpRets[2].split("#")[1];
                return true;
            }
        }
        return false;
    }

    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {

    }
}
